package infolayer2;

import infolayer2.filter.InfolayerXmlImport;
import infolayer2.filter.ModelExtractor;
import infolayer2.filter.TemplateFileImport;
import infolayer2.graph.Graph;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


public class Minibase {

  static Graph graph;
  
  private static void updateTemplates(Graph g) throws IOException {
    TemplateFileImport templateImport = new TemplateFileImport(g);
    templateImport.process(new File("ls8/templates"));
  }
  
  public synchronized static Graph getGraph() {
    try {
      if (graph == null) {
        Graph g = new Graph();
        InfolayerXmlImport ls8Import = new InfolayerXmlImport(g, "/ls8/");
        ls8Import.process(new FileInputStream("ls8/instances.xml"));
        updateTemplates(g);
        
        new ModelExtractor(g).run();
        graph = g;
      } else {
        System.out.println("Updating templates...");
        updateTemplates(graph);
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    
    return graph;
  }
  
}
